var params = {
    "rows": 10, //设置每一页的条目数
    "page": 1, //设置当前的页码
    "maxpages": 5, //设置最多显示的页码数
    /** 接口自带参数 */
    "no": "",
    "name": "",
    "teacher_name": ""
};

/**
 * 调用接口获取学生数据
 */
function init() {
    params.rows = parseInt($('#pagesize').val());
    params.no = $('#txtno').val();
    params.name = $('#txtname').val();
    params.teacher_name = $('#txtteachername').val();
    jqPagingInitLogin('/studentclasslist', params, 'divpage', 'settable');
}

/**
 * 绘制表格数据
 * @param obj
 * @returns {boolean}
 */
function settable(obj) {
    var noticeHtml = '';
    if (obj.table.length == 0) {
        noticeHtml += '<tr>';
        noticeHtml += '<td colspan="8">未检索到任何记录</td>';
        noticeHtml += '</tr>';
        $("#tbody").html(noticeHtml);
        return false;
    }
    for (var i = 0; i < obj.table.length; i++) {
        let studentClass = obj.table[i];
        let id = studentClass.id;
        let no = studentClass.class_no;
        let name = studentClass.class_name;
        let grade = studentClass.grade;

        let teacher_name = studentClass.teacher_name;
        let class_slogan = studentClass.class_slogan;
        let student_count = studentClass.student_count;
        noticeHtml += `<tr>
                        <td>${((i + 1) + (params.rows * (params.page - 1)))} </td>
                        <td name="no">${no}</td>
                        <td name="name">${name}</td>
                        <td name="grade">${grade}</td>
                        <td name="teacher_name">${teacher_name}</td>
                        <td name="class_slogan">${class_slogan}</td>
                        <td name="student_count">${student_count}</td>
                        <td>
                            <button class="btn btn-link" onclick="update('${id}')">编辑</button>
                            <button class="btn btn-link" onclick="deleteClass('${id}')">删除</button>
                            <button class="btn btn-link" onclick="addStudent('${id}','${name}','${grade}')">学生管理</button>
                        </td>
                    </tr>`;
    }
    $("#tbody").html(noticeHtml);
}

/**
 * 弹出添加用户信息框
 */
function addClass() {
    layer.open({
        type: 1,
        title: '新增班级信息',
        closeBtn: 1, //显示关闭按钮
        shadeClose: false,
        area: ['590px', '500px'],
        cancel:function (){
            //不管是新增还是更新后都清空表单
            $("#no").val('');
            $("#name").val('');
            $("#grade").val('');
            $("#teacher_name").val('');
            $("#slogan").val('');
            $("#hidNo").val('');
            isUpdate = false;
        },
        content: $("#div_add")
    });
}

/***
 * 删除用户信息
 */
function deleteClass(id) {
    layer.confirm('确认要删除这个班级吗？', {
        btn: ['是', '否'] //按钮
    }, function () {
        var data = {
            id: id
        }
        var result = ApiPostLogin('/deletestudentclass', JsonToStr(data));
        if (result != -1) {
            console.log(result);
            var obj = eval('(' + result + ')');
            if (obj.error == "1") {
                layer.msg('删除成功', {icon: 1, time: 2000}, function () {
                    init();
                    layer.closeAll();
                });
            } else {
                layer.msg('删除失败，该班级下还有学生，请移除所有学生再进行删除！', {icon: 1, time: 2000}, function () {
                    layer.closeAll();
                });
            }
        }
    }, function () {
    });

}

/**
 * 是否是更新状态
 */
var isUpdate = false;

/**
 * 弹出更新用户框
 * @param {学生ID} no
 */
function update(id) {
    $("#hidNo").val(id);
    isUpdate = true;
    let flag = initData();
    if (flag) {
        layer.open({
            type: 1,
            title: '修改班级信息',
            closeBtn: 1, //显示关闭按钮
            shadeClose: false,
            area: ['590px', '500px'],
            cancel:function (){
                //不管是新增还是更新后都清空表单
                $("#no").val('');
                $("#name").val('');
                $("#grade").val('');
                $("#teacher_name").val('');
                $("#slogan").val('');
                $("#hidNo").val('');
                isUpdate = false;
            },
            content: $("#div_add")
        });
    } else {
        layer.msg('该班级不存在', {icon: 2, time: 2000});
    }
}

/**
 * 加载一条用户数据
 */
function initData() {
    let exists = false;
    let id = $("#hidNo").val();
    var data = {
        id: id
    }
    var result = ApiPostLogin('/getstudentclass', JsonToStr(data));
    if (result != -1) {
        var obj = eval('(' + result + ')');
        if (obj.error == "1") {
            exists = true;
            let data = obj.data;
            $("#no").val(data.class_no);
            $("#no").attr("readonly", true);
            $("#name").val(data.class_name);
            $("#grade").val(data.grade);
            $("#teacher_name").val(data.teacher_name);
            $("#slogan").val(data.class_slogan);
        } else {
            layer.msg('失败');
        }
    }
    return exists;
}

/**
 * 提交数据
 */
function submit() {
    let no = $("#no").val();
    if (no.trim() === "") {
        layer.msg('班级编号不能为空', {icon: 2, time: 2000});
        $("#no").focus();
        return false;
    }
    let name = $("#name").val();
    if (name.trim() === "") {
        layer.msg('班级名称不能为空', {icon: 2, time: 2000});
        $("#name").focus();
        return false;
    }
    let grade = $("#grade").val();
    if (grade.trim() === "") {
        layer.msg('年级不能为空', {icon: 2, time: 2000});
        $("#grade").focus();
        return false;
    }
    let teacher_name = $("#teacher_name").val();
    if (teacher_name.trim() === "") {
        layer.msg('班主任姓名不能为空', {icon: 2, time: 2000});
        $("#teacher_name").focus();
        return false;
    }
    let slogan = $("#slogan").val();

    /**
     * 更新状态只需要更新表格内数据
     */
    if (isUpdate) {
        var data = {
            id: $("#hidNo").val(),
            no: no,
            name: name,
            grade: grade,
            teacher_name: teacher_name,
            class_slogan: slogan
        }
        var result = ApiPostLogin('/updatestudentclass', JsonToStr(data));
        if (result != -1) {
            console.log(result);
            var obj = eval('(' + result + ')');
            if (obj.error == "1") {
                layer.msg('更新成功', {icon: 1, time: 2000}, function () {
                    init();
                    layer.closeAll();
                });
            } else {
                layer.msg('失败');
            }
        }
    } else {
        var data = {
            no: no,
            name: name,
            grade: grade,
            teacher_name: teacher_name,
            class_slogan: slogan
        }
        var result = ApiPostLogin('/addstudentclass', JsonToStr(data));
        if (result != -1) {
            console.log(result);
            var obj = eval('(' + result + ')');
            if (obj.error == "1") {

                layer.msg('新增成功', {icon: 1, time: 2000}, function () {
                    init();
                    layer.closeAll();
                });
            } else {
                layer.msg('失败');
            }
        }
    }
    //不管是新增还是更新后都清空表单
    $("#no").val('');
    $("#name").val('');
    $("#grade").val('');
    $("#teacher_name").val('');
    $("#slogan").val('');
    $("#hidNo").val('');
    isUpdate = false;
}

/**
 * 学生管理
 * @param id
 */
var movein = new Object();
function addStudent(id,name,grade) {
    movein = layer.open({
        type: 2,
        title: '查看班级学生（班级:' + name + "  丨年级:" + grade+"）",
        content: 'classstudent.jsp?classId=' + id ,
        area: ['520px', '520px'],
        maxmin: true,
        end: function () {
            init();
        }
    });
    layer.full(movein);
}
$(function () {
    init();
    /**
     * 绑定新增按钮事件
     */
    $("#btnAdd").click(function () {
        addClass();
    });
    /**
     * 绑定查询按钮事件方法
     */
    $("#btnQuery").click(function () {
        init();
    });
    /**
     * 绑定提交按钮事件方法
     */
    $("#btn_submit").click(function () {
        submit();
    });

});